import type { Directive } from 'vue';
import { useAuthStore } from '@/store/modules/auth';
/**
 * 用法：v-perm="'system:user:list'" 或 v-perm="['a','b']"
 * 不创建页面，此指令仅作为能力注入
 */
export const permDirective: Directive = {
  mounted(el, binding) {
    const auth = useAuthStore();
    const value = binding.value;
    let visible = true;
    if (typeof value === 'string') {
      visible = auth.hasPerm(value);
    } else if (Array.isArray(value)) {
      visible = auth.hasAllPerm(value);
    }
    if (!visible) {
      el.parentNode && el.parentNode.removeChild(el);
    }
  },
};
export default permDirective;
